package com.liangzhong.modules.chat.mediator
{
	import com.common.utils.Reflection;
	import com.liangzhong.constants.NotificationConstants;
	import com.liangzhong.events.ChatEvent;
	import com.liangzhong.mediator.BaseMediator;
	import com.liangzhong.proxyconstant.chat.ChatProxyConstant;
	import com.lz.football.module.chat.IChatView;
	import com.lz.football.module.chat.INoticeView;
	
	import flash.display.DisplayObject;
	
	import org.puremvc.as3.interfaces.INotification;
	
	/**
	 * 聊天 
	 * @author lake
	 * 
	 */	
	public class ChatMediator extends BaseMediator
	{
		public static const MediatorName :String = "ChatMediator";
		/**
		 *通知 
		 */		
		private var _notic:INoticeView;
		
		public function ChatMediator(mediatorName:String=null, viewComponent:Object=null, param:Object=null)
		{
			super(mediatorName, viewComponent);
		}
		/**
		 *获取 实例 
		 * @return 
		 * 
		 */		
		public function get view():IChatView
		{
			return viewComponent as IChatView;
		}
		/**
		 *添加事件 
		 * 
		 */		
		override public function addEventListeners():void
		{
			(view as DisplayObject).addEventListener(ChatEvent.SEND_MESSAGE,onSendMessageHandler);
		}
		
		/**
		 * 发送聊天信息 
		 * @param event
		 * 
		 */
		protected function onSendMessageHandler(event:ChatEvent):void
		{
			this.sendNotification(ChatProxyConstant.CHAT_MEDIATOR_NOTIFICATION,event.param,ChatProxyConstant.SEND_MESSAGE);
		}
		/**
		 * 删除事件 
		 * 
		 */
		override public function removeEventListeners():void
		{
			
		}
		/**
		 * 监听信息
		 * @return 
		 * 
		 */		
		override public function listNotificationInterests():Array
		{
			return [ChatProxyConstant.GET_CHAT_MESSAGE,
				ChatProxyConstant.GET_NOTIC].concat(super.listNotificationInterests());
		}
		/**
		 * 处理信息
		 * @param notification
		 * 
		 */		
		override public function handleNotification(notification:INotification):void
		{
			switch(notification.getName())
			{
				case ChatProxyConstant.GET_CHAT_MESSAGE://获得聊天信息
					view.getMessage(notification.getBody());
					break;
				case ChatProxyConstant.GET_NOTIC:
					showNoticView(notification.getBody());
					break;
				
			}
		}
		/**
		 * 显示公告
		 * 
		 */
		private function showNoticView($obj:Object):void
		{
			if(_notic == null)
			{
				_notic = Reflection.createInstance("com.lz.football.module.chat.NoticeView");
				_notic.initUI();
			}
			_notic.getNotice($obj);
		}
		/**
		 * 
		 * 注册
		 */		
		override public function onRegister():void
		{
			viewComponent = Reflection.createInstance("com.lz.football.module.chat.ChatView");
			view.initUI();
			this.sendNotification(NotificationConstants.MAIN_SET_CHATPANEL,view);
			addEventListeners();
		}
		/**
		 * 删除 mediator
		 * 
		 */		
		override public function removeMediator():void
		{
			facade.removeMediator(MediatorName);
		}
	}
}